package com.tarena.security.admin.controller;

import com.tarena.security.admin.assemble.MicroServiceAssemble;
import com.tarena.security.admin.entity.db.MicroService;
import com.tarena.security.admin.entity.param.MicroServiceParam;
import com.tarena.security.admin.entity.query.MicroServiceQuery;
import com.tarena.security.admin.result.JsonResult;
import com.tarena.security.admin.service.IMicroServiceService;
import com.tarena.security.admin.vo.MicroServiceVO;
import com.tedu.inn.protocol.validation.group.Create;
import com.tedu.inn.protocol.validation.group.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * micro_service 前端控制器
 * </p>
 *
 * @author Ly
 * @since 2023-04-10
 */
@RestController
@Api(tags = "microService")
@RequestMapping("/microService")
public class MicroServiceController {
    @Resource
    private IMicroServiceService microServiceService;

    @Resource
    private MicroServiceAssemble microServiceAssemble;

    @PostMapping("/add-new")
    @ApiOperation("新增MicroService")
    public JsonResult<String> addNewMicroService(@Validated(Create.class) MicroServiceParam microServiceParam){
        microServiceService.addNewMicroService(microServiceParam);
        return JsonResult.ok("新增成功");
    }
    @ApiOperation("获取MicroService列表")
    @GetMapping("/get-MicroService-List")
    public JsonResult<List<MicroServiceVO>> getAppList(MicroServiceQuery microServiceQuery){
        List<MicroService> list=microServiceService.getMicroServiceList(microServiceQuery);
        List<MicroServiceVO> vos = microServiceAssemble.assembleDtosToVos(list);
        return JsonResult.ok(vos);
    }

    @ApiOperation("修改MicroService")
    @PostMapping("/update")
    public JsonResult<String> updateMicroService(@Validated(Update.class) MicroServiceParam microServiceParam){
        microServiceService.updateByID(microServiceParam);
        return JsonResult.ok("修改成功");
    }

    @ApiOperation("根据ID获取MicroService")
    @GetMapping("/getById/{id}")
    public JsonResult<MicroServiceVO> getAppById(@PathVariable Integer id){
        MicroService microService=microServiceService.getMicroServiceById(id);
        MicroServiceVO vo = microServiceAssemble.assembleDtosToVo(microService);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据ID删除MicroService")
    @PostMapping("/deleteById")
    public JsonResult<String> deleteMicroServiceById(Integer ... id){
        microServiceService.deleteMicroServiceById(id);
        return JsonResult.ok("删除成功");
    }
}
